import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/wroks/order_item_info.dart';
import 'package:app/src/http/bean/wroks/product_bean.dart';
import 'package:app/src/http/bean/wroks/special_clauses.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/a_combined_utils.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../app_ui.dart';

///架卡进度
class UiShelftProcess {
  ///List<OrderItemInfo>? 订单条目信息
  ///Product 产品信息
  static Widget build(BuildContext context, Product? orderInfo,
      {String? title}) {
    var data = orderInfo?.OrderShelfStat;
    var productinng = orderInfo?.isProduting() ?? false; //判断是否正在生产中
    if (!productinng) {
      return AppUi.getEmptyView();
    }
    if (StringUtils.isEmpty(data) ||
        StringUtils.isEmpty(orderInfo) ||
        StringUtils.isEmpty(data?.ShelfStatList)) {
      return AppUi.getEmptyView();
    }
    List<Widget> children = [];
    if (StringUtils.isEmpty(title)) {
      title = AppStrings.of(context).ShelfProcess; //架卡进度
    }
    children.add(
        UiBase.title(context, title, bottom: AppEdgeInsets.margin_bottom)); //标题
    if (data != null) {
      children.add(_table(context, data));
    }
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  static Widget _table(
    BuildContext context,
    ProductOrderShelfStat data,
  ) {
    return StatefulBuilder(
      builder: (BuildContext context, StateSetter setState) {
        List<TableRow> children = [];
        //==========================================================================标题
        children.add(TableRow(
            //第一行，添加背景色
            decoration:
                BoxDecoration(color: AppColors.backgroundColorWithTableTitle),
            children: _titles(context, data)));
        if (data != null &&
            data.ShelfStatList != null &&
            data.ShelfStatList!.length > 0) {
          var postion = 1;
          data?.ShelfStatList?.forEach((element) {
            //======================================================================内容
            var cells = _contents(context, element, postion);
            postion++;
            children.add(TableRow(
                children: cells,
                decoration: BoxDecoration(
                    color: AppColors.getColorWithTableRow(postion))));
          });
        }

        ///合计
        if (data != null) {
          children.add(TableRow(
              //最后一行，添加背景色
              decoration:
                  BoxDecoration(color: AppColors.backgroundColorWithTableAcom),
              children: _aCombined(context, data!)));
        }
        var table = Table(
          defaultVerticalAlignment: TableCellVerticalAlignment.middle,
          columnWidths: {
            0: FixedColumnWidth(50),
            1: FixedColumnWidth(180),
          },
          border: AppStyle.getTableBorder(),
          textDirection: TextDirection.ltr,
          children: children,
        );
        var width = 700.0;
        var length = data.HeaderNames?.length;
        if (length != null && length > 0) {
          width = width + (length * 100);
        }
        if (width < px.screenWidth()) {
          width = px.screenWidth();
        }
        return SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          physics: BouncingScrollPhysics(),
          child: Container(
            width: width,
            child: table,
          ),
        );
      },
    );
  }

  ///标题
  static List<Widget> _titles(
      BuildContext context, ProductOrderShelfStat orderInfo) {
    List<Widget> children = [];
    children.add(_addItem(AppStrings.of(context).TheSerial)); //序号
    children.add(_addItem(AppStrings.of(context).LevelShelfId)); //架卡编号
    children.add(_addItem(AppStrings.of(context).PlanGradeName)); //生产等级
    children.add(_addItem(AppStrings.of(context).ItemQuantity)); //数量
    children.add(_addItem(AppStrings.of(context).ProcessName)); //当前工序
    children.add(_addItem(AppStrings.of(context).StartDate)); //排产日期
    orderInfo?.HeaderNames?.forEach((element) {
      children.add(_addItem(element));
    });
    return children;
  }

  ///内容
  static List<Widget> _contents(
      BuildContext context, ShelfStat orderInfo, int postion) {
    List<Widget> children = [];
    children.add(_addItem(postion.toString())); //序号
    children.add(_addItem(orderInfo.LevelShelfId)); //架卡编号
    children.add(_addItem(orderInfo.PlanGradeName)); //生产等级
    children.add(_addItem(orderInfo.ItemQuantity)); //数量
    children.add(_addItem(orderInfo.ProcessName)); //当前工序
    children.add(_addItem(orderInfo.StartDate)); //排产日期
    orderInfo?.ProcessData?.forEach((element) {
      children.add(_addItem(element));
    });
    return children;
  }

  ///合计
  static List<Widget> _aCombined(
      BuildContext context, ProductOrderShelfStat orderInfo) {
    List<Widget> children = [];
    var data = orderInfo?.CombineDatas; //fixme 合计数据
    if (data != null && data.length > 0) {
      children.add(_addItem(AppStrings.of(context).ACombined)); //合计
      children.add(_addItem("")); //架卡编号
      children.add(_addItem("")); //生产等级
      children.add(_addItem(data[0])); //fixme 第一个是 数量合计
      children.add(_addItem("")); //当前工序
      children.add(_addItem("")); //排产日期
      //print("===============================字段个数：${data?.length}\t${orderInfo?.HeaderNames?.length}\t${orderInfo?.ShelfStatList?[0].ProcessData?.length}");
      var headerNames = orderInfo?.HeaderNames;
      if (headerNames != null && headerNames.length > 0) {
        for (int i = 0; i < headerNames!.length; i++) {
          if (i < headerNames.length) {
            if (i == 0) {
              children.add(_addItem("")); //fixme 第一个是 数量合计
            } else {
              if (i < data.length) {
                children.add(_addItem(data[i])); //fixme 后面的是工序合计
              } else {
                children.add(_addItem(""));
              }
            }
          }
        }
      }
    }
    return children;
  }

  static Widget _addItem(String? text,
      {bool isBlue = false, GestureTapCallback? onTap}) {
    Widget child = Center(child: _text(text, isBlue: isBlue));
    if (onTap == null) {
      return child;
    }
    return InkWell(
      child: child,
      onTap: onTap,
    );
  }

  static Widget _text(String? text, {bool isBlue = false}) {
    TextStyle? style = null;
    if (isBlue) {
      style = AppStyle.textStyleWithCardBlue();
    } else {
      style = AppStyle.textStyleWithCard();
    }
    return Text(
      text ?? "",
      style: style,
    );
  }

  ///小图标显示
  static Widget _icon(String? url, GestureTapCallback? onTap) {
    if (StringUtils.isEmpty(url)) {
      return AppUi.getEmptyView();
    }
    return InkWell(
      child: Image.network(
        url!,
        height: 40,
      ),
      onTap: () {
        onTap?.call();
      },
    );
  }
}
